﻿
using MediatR;
using Production_Get.API.API.Application.Commands.Procurement;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Domans.Produce.Resources;
using THE_WIND_RISES.Common.Domans.Produce.WorkCenter_;
using THE_WIND_RISES.Common.Dtos;


namespace Production_Get.API.Application.Commands.Procurement
{
    public class OrderLinesSelIdHandler : IRequestHandler<OrderLinesSelIdCommand, Result>
    {
        private readonly IBaseRepositoryGet<OrderLines> OrderLinesRepository;
        private readonly ILogger<OrderLinesSelIdHandler> logger;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="OrderLinesRepository"></param>
        public OrderLinesSelIdHandler(ILogger<OrderLinesSelIdHandler> logger, IBaseRepositoryGet<OrderLines> OrderLinesRepository)
        {
            this.logger = logger;
            this.OrderLinesRepository = OrderLinesRepository;
        }
        public async Task<Result> Handle(OrderLinesSelIdCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };
            try
            {
                string sql= $"select * from OrderLines WHERE OrderLineId = {request.OrderLineId}";
                var list = OrderLinesRepository.GetAll(sql, default);
                if (list!=null)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = list;
                    result.Msg = "反填信息成功";
                    logger.LogInformation($"反填订单行信息成功");
                }
                else
                {
                    result.Code = (int)CommonErrorCodes.Search;
                    result.Data = null;
                    result.Msg = "反填信息失败";
                    logger.LogError($"反填订单行信息失败");
                }
            }
            catch (Exception ex)
            {
                logger.LogError($"反填订单行信息失败,错误信息为：" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
